package utilities;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import p2pExceptions.FileUnreadableException;
import p2pExceptions.NoScannerException;

/**
 *
 * @author zmasood
 */
public class ReadFile {

    public ReadFile(){
        fileOpen = false;
        readyForRead = false;
        text = null;
        scanner = null;
        fstream = null;
        file = null;
    }

    public ReadFile(File f) {
        file = f;
        if (!file.exists() || !file.canRead()) {
            file = null;
            
        }
        try {
            fstream = new FileInputStream(file);
        }
        catch (IOException e) {e.printStackTrace();}
        scanner = new Scanner(fstream);
        fileOpen = true;
        readyForRead = true;
    }

    public ReadFile(String path) {
        file = new File(path);
        if (!file.exists() || !file.canRead()) {
            file = null;
        }
        else {
            try {
                fstream = new FileInputStream(file + ".chs");
                scanner = new Scanner(fstream);
                fileOpen = true;
                readyForRead = true;
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void close() throws IOException{
        file = null;
        try {
            fstream.close();
            scanner.close();
            fileOpen = false;
            readyForRead = false;
        } catch (IOException ex) {
            Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }
    }

    public String readLine() throws EOFException, IOException{
        try {
            if (scanner.hasNextLine()) {
                return scanner.nextLine();
            }
            close();
            throw new EOFException();
        } catch (IOException ex) {
            Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }
    }

    public String readFile(){
        try {
            text = new StringBuilder();
            while (scanner.hasNextLine()) {
                text.append(scanner.nextLine()).append(newLine);
                hash = scanner.nextLine();
            }
            close();
        } catch (IOException ex) {
            Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE, null, ex);
        }
        return hash;
    }

    public boolean isReadyForRead(){
        return readyForRead;
    }

    public boolean isFileOpen(){
        return fileOpen;
    }

    public Scanner getScanner() throws NoScannerException{
        if(scanner==null){
            throw new NoScannerException();
        }
        return scanner;
    }

    final String newLine = System.getProperty("line.separator");
    StringBuilder text = null;
    private Scanner scanner = null;
    private FileInputStream fstream = null;
    private File file = null;
    private boolean fileOpen = false;
    private boolean readyForRead = false;
    private String hash;
}
